Trò chơi Tic-Tac-Toe, game đánh caro full source code
- Room.cs
- PhotonNetwork /
- Plugins /
- Photon Unity Networking /
- Assets /
- project /
2 // <copyright file="Room.cs" company="Exit Games GmbH">
3 // PhotonNetwork Framework for Unity - Copyright (C) 2011 Exit Games GmbH
4 // </copyright>
5 // <summary>
6 // Represents a room/game on the server and caches the properties of that.
7 // </summary>
8 // <author>developer@exitgames.com</author>
9 // ----------------------------------------------------------------------------
10
11 using System;
12 using ExitGames.Client.Photon;
13 using UnityEngine;
14
15
16 /// <summary>
17 /// This class resembles a room that PUN joins (or joined).
18 /// The properties are settable as opposed to those of a RoomInfo and you can close or hide "your" room.
19 /// </summary>
20 /// \ingroup publicApi
21 public class Room : RoomInfo
22 {
23 /// <summary>Count of players in this room.</summary>
24 public new int playerCount
25 {
26 get
27 {
28 if (PhotonNetwork.playerList != null)
29 {
30 return PhotonNetwork.playerList.Length;
31 }
32 else
33 {
34 return 0;
35 }
36 }
37 }
38
39
40 /// <summary>The name of a room. Unique identifier (per Loadbalancing group) for a room/match.</summary>
41 public new string name
42 {
43 get
44 {
45 return this.nameField;
46 }
47
48 internal set
49 {
50 this.nameField = value;
51 }
52 }
53
54 /// <summary>
55 /// Sets a limit of players to this room. This property is shown in lobby, too.
56 /// If the room is full (players count == maxplayers), joining this room will fail.
57 /// </summary>
58 public new int maxPlayers
59 {
60 get
61 {
62 return (int)this.maxPlayersField;
63 }
64
65 set
66 {
67 if (!this.Equals(PhotonNetwork.room))
68 {
69 UnityEngine.Debug.LogWarning("Can't set maxPlayers when not in that room.");
70 }
71
72 if (value > 255)
73 {
74 UnityEngine.Debug.LogWarning("Can't set Room.maxPlayers to: " + value + ". Using max value: 255.");
75 value = 255;
76 }
77
78 if (value != this.maxPlayersField && !PhotonNetwork.offlineMode)
79 {
80 PhotonNetwork.networkingPeer.OpSetPropertiesOfRoom(new Hashtable() { { GameProperties.MaxPlayers, (byte)value } }, true, (byte)0);
81 }
82
83 this.maxPlayersField = (byte)value;
84 }
85 }
86
87 /// <summary>
88 /// Defines if the room can be joined.
89 /// This does not affect listing in a lobby but joining the room will fail if not open.
90 /// If not open, the room is excluded from random matchmaking.
91 /// Due to racing conditions, found matches might become closed before they are joined.
92 /// Simply re-connect to master and find another.
93 /// Use property "visible" to not list the room.
94 /// </summary>
95 public new bool open
96 {
97 get
98 {
99 return this.openField;
100 }
101
102 set
103 {
104 if (!this.Equals(PhotonNetwork.room))
105 {
106 UnityEngine.Debug.LogWarning("Can't set open when not in that room.");
107 }
108
109 if (value != this.openField && !PhotonNetwork.offlineMode)
110 {
111 PhotonNetwork.networkingPeer.OpSetPropertiesOfRoom(new Hashtable() { { GameProperties.IsOpen, value } }, true, (byte)0);
112 }
113
114 this.openField = value;
115 }
116 }
117
118 /// <summary>
119 /// Defines if the room is listed in its lobby.
120 /// Rooms can be created invisible, or changed to invisible.
121 /// To change if a room can be joined, use property: open.
122 /// </summary>
123 public new bool visible
124 {
125 get
126 {
127 return this.visibleField;
128 }
129
130 set
131 {
132 if (!this.Equals(PhotonNetwork.room))
133 {
134 UnityEngine.Debug.LogWarning("Can't set visible when not in that room.");
135 }
136
137 if (value != this.visibleField && !PhotonNetwork.offlineMode)
138 {
139 PhotonNetwork.networkingPeer.OpSetPropertiesOfRoom(new Hashtable() { { GameProperties.IsVisible, value } }, true, (byte)0);
140 }
141
142 this.visibleField = value;
143 }
144 }
145
146 /// <summary>
147 /// A list of custom properties that should be forwarded to the lobby and listed there.
148 /// </summary>
149 public string[] propertiesListedInLobby { get; private set; }
150
151 /// <summary>
152 /// Gets if this room uses autoCleanUp to remove all (buffered) RPCs and instantiated GameObjects when a player leaves.
153 /// </summary>
154 public bool autoCleanUp
155 {
156 get
157 {
158 return this.autoCleanUpField;
159 }
160 }
161
162 internal Room(string roomName, RoomOptions options) : base(roomName, null)
163 {
164 if (options == null)
165 {
166 options = new RoomOptions();
167 }
168
169 this.visibleField = options.isVisible;
170 this.openField = options.isOpen;
171 this.maxPlayersField = (byte)options.maxPlayers;
172 this.autoCleanUpField = false; // defaults to false, unless set to true when room gets created.
173
174 this.CacheProperties(options.customRoomProperties);
175 this.propertiesListedInLobby = options.customRoomPropertiesForLobby;
176 }
177
178 /// <summary>
179 /// Updates and synchronizes the named properties of this Room with the values of propertiesToSet.
180 /// </summary>
181 /// <remarks>
182 /// Any player can set a Room's properties. Room properties are available until changed, deleted or
183 /// until the last player leaves the room.
184 /// Access them by: Room.CustomProperties (read-only!).
185 ///
186 /// To reduce network traffic, set only values that actually changed.
187 ///
188 /// New properties are added, existing values are updated.
189 /// Other values will not be changed, so only provide values that changed or are new.
190 /// To delete a named (custom) property of this room, use null as value.
191 /// Only string-typed keys are applied (everything else is ignored).
192 ///
193 /// Local cache is updated immediately, other clients are updated through Photon with a fitting operation.
194 /// </remarks>
195 /// <param name="propertiesToSet">Hashtable of props to udpate, set and sync. See description.</param>
196 public void SetCustomProperties(Hashtable propertiesToSet)
197 {
198 if (propertiesToSet == null)
199 {
200 return;
201 }
202
203 // merge (delete null-values)
204 this.customProperties.MergeStringKeys(propertiesToSet); // includes a Equals check (simplifying things)
205 this.customProperties.StripKeysWithNullValues();
206
207
208 // send (sync) these new values
209 Hashtable customProps = propertiesToSet.StripToStringKeys() as Hashtable;
210 if (!PhotonNetwork.offlineMode)
211 {
212 PhotonNetwork.networkingPeer.OpSetCustomPropertiesOfRoom(customProps, true, 0);
213 }
214 NetworkingPeer.SendMonoMessage(PhotonNetworkingMessage.OnPhotonCustomRoomPropertiesChanged, propertiesToSet);
215 }
216
217 /// <summary>
218 /// Enables you to define the properties available in the lobby if not all properties are needed to pick a room.
219 /// </summary>
220 /// <remarks>
221 /// It makes sense to limit the amount of properties sent to users in the lobby as this improves speed and stability.
222 /// </remarks>
223 /// <param name="propsListedInLobby">An array of custom room property names to forward to the lobby.</param>
224 public void SetPropertiesListedInLobby(string[] propsListedInLobby)
225 {
226 Hashtable customProps = new Hashtable();
227 customProps[GameProperties.PropsListedInLobby] = propsListedInLobby;
228 PhotonNetwork.networkingPeer.OpSetPropertiesOfRoom(customProps, false, 0);
229
230 this.propertiesListedInLobby = propsListedInLobby;
231 }
232
233
234 /// <summary>Returns a summary of this Room instance as string.</summary>
235 /// <returns>Summary of this Room instance.</returns>
236 public override string ToString()
237 {
238 return string.Format("Room: '{0}' {1},{2} {4}/{3} players.", this.nameField, this.visibleField ? "visible" : "hidden", this.openField ? "open" : "closed", this.maxPlayersField, this.playerCount);
239 }
240
241 /// <summary>Returns a summary of this Room instance as longer string, including Custom Properties.</summary>
242 /// <returns>Summary of this Room instance.</returns>
243 public new string ToStringFull()
244 {
245 return string.Format("Room: '{0}' {1},{2} {4}/{3} players.\ncustomProps: {5}", this.nameField, this.visibleField ? "visible" : "hidden", this.openField ? "open" : "closed", this.maxPlayersField, this.playerCount, this.customProperties.ToStringFull());
246 }
247 }
----------------------------------------------------------------------------
PhotonNetwork Framework for Unity - Copyright (C) 2011 Exit Games GmbH
Represents a roomgame on the server and caches the properties of that.
----------------------------------------------------------------------------
This class resembles a room that PUN joins (or joined).
The properties are settable as opposed to those of a RoomInfo and you can close or hide "your" room.
\ingroup publicApi
Sets a limit of players to this room. This property is shown in lobby, too.
If the room is full (players count == maxplayers), joining this room will fail.
Defines if the room can be joined.
This does not affect listing in a lobby but joining the room will fail if not open.
If not open, the room is excluded from random matchmaking.
Due to racing conditions, found matches might become closed before they are joined.
Simply re-connect to master and find another.
Use property "visible" to not list the room.
Defines if the room is listed in its lobby.
Rooms can be created invisible, or changed to invisible.
To change if a room can be joined, use property: open.
A list of custom properties that should be forwarded to the lobby and listed there.
Gets if this room uses autoCleanUp to remove all (buffered) RPCs and instantiated GameObjects when a player leaves.
this.autoCleanUpField = false; defaults to false, unless set to true when room gets created.
Updates and synchronizes the named properties of this Room with the values of propertiesToSet.
Any player can set a Room's properties. Room properties are available until changed, deleted or
until the last player leaves the room.
Access them by: Room.CustomProperties (read-only!).
To reduce network traffic, set only values that actually changed.
New properties are added, existing values are updated.
Other values will not be changed, so only provide values that changed or are new.
To delete a named (custom) property of this room, use null as value.
Only string-typed keys are applied (everything else is ignored).
Local cache is updated immediately, other clients are updated through Photon with a fitting operation.
Hashtable of props to udpate, set and sync. See description.
merge (delete null-values)
this.customProperties.MergeStringKeys(propertiesToSet); includes a Equals check (simplifying things)
send (sync) these new values
Enables you to define the properties available in the lobby if not all properties are needed to pick a room.
It makes sense to limit the amount of properties sent to users in the lobby as this improves speed and stability.
An array of custom room property names to forward to the lobby.